from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from django.http import HttpResponse

# Create your views here.
from moments.models import WeChatUser, Status


def home(request):
    return render(request, "homepage.html")


@login_required
def show_user(request):
    """
    po = {
        "username": "xiao po",
        "motto": "i love kungfu",
        "email": "xiaopo@disney.com",
        "region": "Shaanxi",
        "pic": "Po2.jgp",
    }
    """
    user = WeChatUser.objects.get(user=request.user)
    return render(request, "user.html", {"user": user})


@login_required
def show_status(request):
    statuses = Status.objects.all()
    '''
    import pdb
    pdb.set_trace()
    '''
    return render(request, "status.html", {"statuses": statuses})


@login_required
def submit_post(request):
    """
    import pdb
    pdb.set_trace()
    """
    user = WeChatUser.objects.get(user=request.user)
    text = request.POST.get("text")
    uploded_file = request.FILES.get("pics")
    if uploded_file:
        name = uploded_file.name
        with open("./moments/static/image/{}".format(name), "wb") as handler:
            for block in uploded_file.chunks():
                handler.write(block)
    else:
        name = ''
    if text:
        status = Status(user=user, text=text, pics=name)
        status.save()
        return redirect("/status")

    return render(request, "my_post.html")


@login_required
def friend(request):
    return render(request, "friends.html")
